//============================================================================
// Name        : Tester.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

#include <condition_variable>
#include <mutex>
#include <thread>
#include <functional>
#include <memory>
#include <regex>
#include <iterator>
#include <vector>

#include <gtest/gtest.h>
#include <glog/logging.h>
#include <google/protobuf/stubs/common.h>
#include <curl/curl.h>
#include <mysql/mysql.h>

#include <boost/filesystem.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

#include <Framework/Type.h>
#include <Framework/Macro.h>

#include <Framework/Static.h>

#include <TDD/Static.h>

//#include <mongo/client/connpool.h>

std::condition_variable condition_;
std::mutex mutex_;

bool init_glog(INT argc, char **argv)
{
	bool bExistDirectory = boost::filesystem::exists("Logs");
	if (false == bExistDirectory)
	{
		INT nRet_mkdir = mkdir("Logs", S_IFDIR | S_IRWXU | S_IRWXG | S_IXOTH | S_IROTH);
		if (nRet_mkdir < 0)
		{
			return false;
		}
	}

	FLAGS_logtostderr = false;
	FLAGS_stderrthreshold = 0;
	FLAGS_minloglevel = 0;
	FLAGS_log_dir="./Logs";
	FLAGS_max_log_size = 1024;
	::google::InitGoogleLogging(argv[0]);
	::google::InstallFailureSignalHandler();

	return true;
}

void Initial() {
	GOOGLE_PROTOBUF_VERIFY_VERSION;

	// thread safe
	curl_global_init(CURL_GLOBAL_ALL);

	// thread safe
	if (mysql_library_init(0, NULL, NULL)) {
		fprintf(stderr, "could not initialize MySQL library\n");
		exit(1);
  }
}

void Final() {
	mysql_library_end();

	google::protobuf::ShutdownProtobufLibrary();
}

int main(INT argc, char **argv)
{
	Initial();

	bool nRet_Init_glog = init_glog(argc, argv);
	if (false == nRet_Init_glog) return 0;

	::Framework::Static clsStatic;
	::TDD::Static clsTDDStatic;

	INT nRetGoogleTest = clsTDDStatic.gtest(argc, argv);
	if (0 != nRetGoogleTest) return nRetGoogleTest;

	LOG(INFO) << "[ MAIN THREAD ID : " << std::this_thread::get_id() << " ]";
	LOG(INFO) << "----------- START SERVER -----------";

	std::unique_lock<std::mutex> lock(mutex_);
	condition_.wait(lock);

	Final();

	return 0;
}


